package com.hq.fxh.No3_TuiGuang.Adaoter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.hq.fxh.Modle.GetPushStatisticListResMonth;
import com.hq.fxh.R;

import java.util.List;


/**
 * @author 小谢
 * @ClassName PushStatisticGroupAdapter
 * @Description :
 * @Created 2017/5/18 0018
 */

public class PushStatisticGroupAdapter extends RecyclerView.Adapter<PushStatisticGroupAdapter.ViewHolder> {

    private List<GetPushStatisticListResMonth> list;
    private Context mContext;
    private boolean isShowGroupItem = false;

    public PushStatisticGroupAdapter(Context context, List<GetPushStatisticListResMonth> list) {
        this.list = list;
        mContext = context;
    }

    @Override
    public PushStatisticGroupAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_section, viewGroup, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(final PushStatisticGroupAdapter.ViewHolder holder, int position) {
        //设置数据
        String month =list.get(position).getDate().split("-")[1];//"date": "2017-05"
        holder.tv_month.setText(month +"月");
        holder.tv_click.setText("点击 " + list.get(position).getTot_click());
        holder.tv_look.setText("曝光 " + list.get(position).getTot_look());
        //初始化列表形式
        holder.mGroup.setVisibility(View.GONE);
       // holder.img_button.setImageResource(R.drawable.ht_zhengchang);
        isShowGroupItem = false;
        //创建默认的线性LayoutManager
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        //显示底部位置
        mLayoutManager.setStackFromEnd(true);
        holder.mGroup.setLayoutManager(mLayoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        holder.mGroup.setHasFixedSize(true);
        PushStatisticItemAdapter groupItemAdapter = new PushStatisticItemAdapter(list.get(position).getData());
        holder.mGroup.setAdapter(groupItemAdapter);
        holder.ll_group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isShowGroupItem) {//展开
                    holder.mGroup.setVisibility(View.VISIBLE);
                     isShowGroupItem = true;
                } else {//关闭
                    holder.mGroup.setVisibility(View.GONE);
                     isShowGroupItem = false;
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.isEmpty() ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
         TextView tv_month, tv_click, tv_look;
        RecyclerView mGroup;
        LinearLayout ll_group;

        public ViewHolder(View itemView) {
            super(itemView);
             tv_month = (TextView) itemView.findViewById(R.id.tv_month);
            tv_click = (TextView) itemView.findViewById(R.id.tv_click);
            tv_look = (TextView) itemView.findViewById(R.id.tv_look);
            mGroup = (RecyclerView) itemView.findViewById(R.id.rl_group);
            ll_group = (LinearLayout) itemView.findViewById(R.id.ll_group);
        }
    }
}
